package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BatteryLog;
import com.tbit.main.pojo.PowerChangeLogExpand;
import com.tbit.main.pojo.PowerChangeLogSta;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 电池操作日志
 *
 * @author LMY
 * @create 2021-05-27 15:28
 */
@RestController
@RequestMapping("/batteryLogController")
public class BatteryLogController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private BatteryLogService batteryLogService;
    @Autowired
    private DataService dataService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private BatteryService batteryService;
    @Autowired
    private PowerChangeLogExpandService powerChangeLogExpandService;

    /**
     * 根据电池编号查询操作记录
     */
    @RequestMapping("/batteryNO")
    public JsonResult batteryNO(String token, String batteryNO, String startTime, String endTime) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        Integer accountId = dataService.getBatteryAccountId(batteryNO);

        batteryService.auth(accountUserToken.getAgents(), dataService.getBatteryId(batteryNO));

        List<BatteryLog> batteryLogs = batteryLogService.bateryNO(accountId, batteryNO, startTime, endTime);

        return JsonResult.succ(batteryLogs);
    }

    /**
     * 根据人员查询操作记录
     */
    @RequestMapping("/userId")
    public JsonResult userId(Integer accountId, Integer userMainId, String startTime, String endTime) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<BatteryLog> batteryLogs = batteryLogService.userId(accountId, userMainId, startTime, endTime);

        return JsonResult.succ(batteryLogs);
    }

    /**
     * 根据换电记录查询换电记录
     */
    @RequestMapping("/powerChangeId")
    public JsonResult powerChangeId(Integer powerChangeId, Integer mapType) {

        Assert.notNull(mapType, "Battery.type.notNull");

        PowerChangeLogExpand powerChangeLogExpand = powerChangeLogExpandService.getPowerChangeId(powerChangeId);

        if (powerChangeLogExpand != null) {
            calibrateService.calibrateOffSet(powerChangeLogExpand, mapType);
        }

        return JsonResult.succ(powerChangeLogExpand);
    }

    /**
     * 根据类型查询记录分组
     */
    @RequestMapping("/typeGroup")
    public JsonResult typeGroup(String token, Integer accountId, String startTime, String endTime, Integer type) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<PowerChangeLogSta> powerChangeLogStas = batteryLogService.groupByTime(accountId, accountUserToken.getAccountUserType(), accountUserToken.getAccountUserId(), type, startTime, endTime);

        return JsonResult.succ(powerChangeLogStas);
    }

    /**
     * 根据类型查询记录详情
     */
    @RequestMapping("/typeGroupTime")
    public JsonResult typeGroupTime(String token, Integer accountId, Integer userMainId, String startTime, String endTime, Integer type) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<BatteryLog> batteryLogs = batteryLogService.getByTime(accountId, userMainId, type, startTime, endTime);

        return JsonResult.succ(batteryLogs);
    }


}
